package org.homonoia.model.query;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class QueryID implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = -4929035059917201568L;
    volatile private static int counter = 0;

    private String jobID;

    public QueryID() {
	super();

	String id = "job_";
	DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd_HHmmss");
	Date date = new Date();
	id += dateFormat.format(date);
	id += "_" + counter;

	setJobID(id);
	counter++;
    }

    @Override
    public String toString() {
	return jobID;
    }

    private void setJobID(String jobID) {
	this.jobID = jobID;
    }

    private void validateJobID() {
	if (!this.jobID.startsWith("job_")) {
	    throw new IllegalArgumentException("Invalid Job ID");
	}
    }

    private void readObject(ObjectInputStream is) throws ClassNotFoundException, IOException {
	is.defaultReadObject();

	validateJobID();
    }

    private void writeObject(ObjectOutputStream os) throws IOException {
	os.defaultWriteObject();
    }
}
